﻿#pragma once
#include "AsGraphicsBasics.h"
#include "AsGraphics.h"
#include "AsGraphicsEvents.h"

__AS_JY(Update 2023.1.7)
class AS_GRAPHICS_API AsGraphicsController : public AsBinder<AsGraphics> {
public:
	AsGraphicsController();
	explicit AsGraphicsController(AsGraphics* core);
	AsGraphicsController(const AsGraphicsController& con);
	~AsGraphicsController();
	AsGraphicsController& operator=(const AsGraphicsController& con);

private:
	AS_MEMBER_CLASS(AsGraphicsController)* m;

public:
	AsGraphics* 				core() const;
	void						setCore(AsGraphics* core);
	int							width();
	int							height();
	QPointF						gTopLeft();
	QPointF						gTopRight();
	QPointF						gBottomLeft();
	QPointF						gBottomRight();
	double						gWidth();
	double						gHeight();
	QRectF						gRect();

public:
	QGraphicsScene*				getScene();
	void						setSceneSize(const QSizeF& size);
	void						setViewScale(double sx, double sy);
	void						chgViewScale(double sx, double sy);
	void						setViewPosition(const QPointF& pos);
	void						movViewPosition(const QPointF& mov);
	void						movViewPosition(double mx, double my);
	void						setBackgroundColor(const QColor& color);
	QSizeF						getSceneSize();
	double						getViewScaleX();
	double						getViewScaleY();
	QPointF						getViewPosition();
	QRectF						getViewRect();
	QRectF						getBoundingRect();
	QPoint						toGraphicPos(const QPoint& widgetPos);
	QPointF						toGraphicPosF(const QPoint& widgetPos);
	QPoint						toMousePos(const QPointF& graphicPos);

public:
	void						connectSynchronizationGraphics(AsGraphics* g, bool mouse_sync = true, bool key_sync = true);
	void						disconnectSynchronizationGraphics(AsGraphics* g);
	void						clearSynchronizationGraphics();

public:
	void						setEnable_ReverseXAxis(bool isEnable = true);
	void						setEnable_ReverseYAxis(bool isEnable = true);

public:
	int							currentGroupEntrance();
	void						setGroupEntrance(int group_id);
	void						recoveryGroupEntrace();
	QGraphicsItem*				getRecentItem();
	AsGraphicsEvents*			getEvents();

public:
	AsGraphicsLineItem*			addLine(const QLineF& line, const QPen& pen = QPen());
	AsGraphicsPolylineItem*		addPolyline(const AG::PolylineF& polyline, const QPen& pen = QPen());
	AsGraphicsRotatedRectItem*	addRotatedRect(const AG::RotatedRect& rotatedRect, const QPen& pen = QPen(), const QBrush& brush = QBrush());
	AsGraphicsImageItem*		addImage(const QImage& image);
	QGraphicsLineItem*			addQLine(const QLineF& line, const QPen& pen = QPen());
	QGraphicsRectItem*			addQRect(const QRectF& rect, const QPen& pen = QPen(), const QBrush& brush = QBrush());
	QGraphicsEllipseItem*		addQEllipse(const QRectF& rect, const QPen& pen = QPen(), const QBrush& brush = QBrush());
	QGraphicsPolygonItem*		addQPolygon(const QPolygonF& polygon, const QPen& pen = QPen(), const QBrush& brush = QBrush());
	QGraphicsSimpleTextItem*	addQSimpleText(const QString& text, const QFont& font = QFont());
	QGraphicsTextItem*			addQText(const QString& text, const QFont& font = QFont());
	QGraphicsPixmapItem*		addQPixmap(const QPixmap& pixmap);

	AsGraphicsLineItem*			addLine(const QPointF& position, const QLineF& line, const QPen& pen = QPen(), AG::ItemOptions itemOption = AG::ItemOption_None);
	AsGraphicsPolylineItem*		addPolyline(const QPointF& position, const AG::PolylineF& polyline, const QPen& pen = QPen(), AG::ItemOptions itemOption = AG::ItemOption_None);
	AsGraphicsRotatedRectItem*	addRotatedRect(const QPointF& position, const AG::RotatedRect& rotatedRect, const QPen& pen = QPen(), const QBrush& brush = QBrush(), AG::ItemOptions itemOption = AG::ItemOption_None);
	AsGraphicsImageItem*		addImage(const QPointF& position, const QImage& image, AG::ItemOptions itemOption = AG::ItemOption_None);
	QGraphicsLineItem*			addQLine(const QPointF& position, const QLineF& line, const QPen& pen = QPen(), AG::ItemOptions itemOption = AG::ItemOption_None);
	QGraphicsRectItem*			addQRect(const QPointF& position, const QRectF& rect, const QPen& pen = QPen(), const QBrush& brush = QBrush(), AG::ItemOptions itemOption = AG::ItemOption_None);
	QGraphicsEllipseItem*		addQEllipse(const QPointF& position, const QRectF& rect, const QPen& pen = QPen(), const QBrush& brush = QBrush(), AG::ItemOptions itemOption = AG::ItemOption_None);
	QGraphicsPolygonItem*		addQPolygon(const QPointF& position, const QPolygonF& polygon, const QPen& pen = QPen(), const QBrush& brush = QBrush(), AG::ItemOptions itemOption = AG::ItemOption_None);
	QGraphicsSimpleTextItem*	addQSimpleText(const QPointF& position, const QString& text, const QFont& font = QFont(), AG::ItemOptions itemOption = AG::ItemOption_None);
	QGraphicsTextItem*			addQText(const QPointF& position, const QString& text, const QColor& color = Qt::white, const QFont& font = QFont(), AG::ItemOptions itemOption = AG::ItemOption_None);
	QGraphicsPixmapItem*		addQPixmap(const QPointF& position, const QPixmap& pixmap, AG::ItemOptions itemOption = AG::ItemOption_None);

	QGraphicsItem*				addGraphic(QGraphicsItem* item);
	void						addGraphics(const QList<QGraphicsItem*>& items);
	void						addGraphics(const QVector<QGraphicsItem*>& items);
	void						removeGraphic(QGraphicsItem* item);
	void						removeGraphics(const QList<QGraphicsItem*>& items);
	void						removeGraphics(const QVector<QGraphicsItem*>& items);
	void 						deleteGraphic(QGraphicsItem* item);
	void 						deleteGraphics(const QList<QGraphicsItem*>& items);
	void 						deleteGraphics(const QVector<QGraphicsItem*>& items);

public:
	inline AsGraphicsLineItem*		addLine(double x1, double y1, double x2, double y2, const QPen& pen = QPen()) { return addLine(QLineF(x1, y1, x2, y2), pen); }
	inline AsGraphicsLineItem*		addLine(const QPointF& p1, const QPointF& p2, const QPen& pen = QPen()) { return addLine(QLineF(p1, p2), pen); }
	inline QGraphicsLineItem*		addQLine(double x1, double y1, double x2, double y2, const QPen& pen = QPen()) { return addQLine(QLineF(x1, y1, x2, y2), pen); }
	inline QGraphicsLineItem*		addQLine(const QPointF& p1, const QPointF& p2, const QPen& pen = QPen()) { return addQLine(QLineF(p1, p2), pen); }
	inline QGraphicsRectItem*		addQRect(double x, double y, double width, double height, const QPen& pen = QPen(), const QBrush& brush = QBrush()) { return addQRect(QRectF(x, y, width, height), pen, brush); }
	inline QGraphicsRectItem*		addQRect(const QPointF& p, const QSizeF& size, const QPen& pen = QPen(), const QBrush& brush = QBrush()) { return addQRect(QRectF(p, size), pen, brush); }
	inline QGraphicsEllipseItem*	addQEllipse(double x, double y, double width, double height, const QPen& pen = QPen(), const QBrush& brush = QBrush()) { return addQEllipse(QRectF(x, y, width, height), pen, brush); }
	inline QGraphicsEllipseItem*	addQEllipse(const QPointF& p, const QSizeF& size, const QPen& pen = QPen(), const QBrush& brush = QBrush()) { return addQEllipse(QRectF(p, size), pen, brush); }

public:
	QVector<QGraphicsItem*>		getGroupItems(int group_id);
	void 						removeGroupGraphics(int group_id);
	void 						deleteGroupGraphics(int group_id);
	void 						removeAllGraphics();
	void 						deleteAllGraphics();

public:
	QPixmap						screenshot(const QRect& roi = QRect(QPoint(0, 0), QSize(-1, -1)));
	QPixmap						render(const QRect& roi);

};

namespace AsGraphicsNamespace {
	typedef AsGraphicsController Controller;
};